Most Dangerous Countries in the World 2022

The GPI report evaluates 163 countries that account for more than 99.7% of the world’s total population. The factors analyzed in the report are grouped into three different areas: Safety and Security, Ongoing Conflict, and Militarization. The factors used to compile this report include: the number of internal and external violent conflicts, level of distrust, politicla instability, potential for terrorist acts, number of homicides, and military expenditures as a percetnage of GDP. A score is calculated for each of the 163 nations featured in the report based on these factors. The higher the score, the more dangerous the country is and the lower it ranks in terms of safety.

Top 20 Most Dangerous Countries in the World (Global Peace Index 2022 — higher scores are less safe)

IPUMS provided the shapefils of all the workd countries

    Ref:  https://international.ipums.org/international/index.shtml

Add libraries

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.5 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(leaflet)
library(tmap)
library(tmaptools)
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(rgdal)
## Loading required package: sp
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-32, (SVN revision 1176)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.4.3, released 2022/04/22
## Path to GDAL shared files: C:/Users/jim_PC_dell/AppData/Local/R/win-library/4.2/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/jim_PC_dell/AppData/Local/R/win-library/4.2/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.5-0
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
library(readxl)
library(stats)
library(spdplyr)
## Warning: package 'spdplyr' was built under R version 4.2.2
library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(mapview)
library(DT)

Global Peace Index

Ref:https://worldpopulationreview.com/country-rankings/most-dangerous-countries
    Take a quick look at the whole dangerous country index
    Some country names are different from the IPUMS Country shapefiles and will meed modification
country_10 <- read_csv("data/csvData.csv") %>%
  select(c(1:3))
## Rows: 162 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): country
## dbl (5): gpi22, dangerRank22, gpi21, dangerRank21, gpiChange
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
datatable(country_10)

Create object

      Rename some variables
      Filter to 10 countries change variables to numeric class
country_10 <- read_csv("data/csvData.csv") %>%
  select(c(1:3)) %>%
  rename(score = gpi22, rank = dangerRank22, CNTRY_NAME = country) %>%
  filter(rank %in% c(1:10)) 
## Rows: 162 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): country
## dbl (5): gpi22, dangerRank22, gpi21, dangerRank21, gpiChange
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
country_10$score <- as.numeric(country_10$score) 
country_10 <- country_10 %>% mutate(Danger.Score = as.numeric(score * 1000))

Select the top 10 most dangerous countries based on the Global Peace Index

# countrys_map from https://international.ipums.org/international/index.shtml
country_sf <- sf::st_read("shp/world_countries_2020.shp") %>%
  filter(CNTRY_NAME %in% c( "Afghanistan", "Yemen", "Syria", "Russia", "South Sudan", "Congo", "Iraq", "Somalia", "Central Africa Republic", "Sudan")|CNTRY_CODE == "140")
## Reading layer `world_countries_2020' from data source 
##   `C:\Users\jim_PC_dell\Desktop\new R files\30 Day Map Challenge 2022\Master\Bad Countries\shp\world_countries_2020.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 284 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -180 ymin: -89.99998 xmax: 180 ymax: 83.63339
## Geodetic CRS:  WGS 84

View country shapefile data

head(country_sf)
## Simple feature collection with 6 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 11.15324 ymin: -5.031389 xmax: 74.88986 ymax: 38.49073
## Geodetic CRS:  WGS 84
##   OBJECTID               CNTRY_NAME CNTRY_CODE BPL_CODE
## 1       11 Central African Republic        140    12030
## 2       18                    Congo        178    12050
## 3       54                  Somalia        706    11140
## 4       55                    Sudan        729    13050
## 5       58              South Sudan        728    11150
## 6       70              Afghanistan        004    32010
##                         geometry
## 1 MULTIPOLYGON (((22.5812 11....
## 2 MULTIPOLYGON (((17.46837 3....
## 3 MULTIPOLYGON (((41.58164 -1...
## 4 MULTIPOLYGON (((38.49294 18...
## 5 MULTIPOLYGON (((33.28475 12...
## 6 MULTIPOLYGON (((71.00445 38...

Merge data frame with sf object to create new sf object

ctry_combined_sf <- merge(country_sf, country_10, duplicateGeoms = T)

Create a map (“Bad Map”) the shows the top 10 most dangerous countries

tmap_mode("view") 
## tmap mode set to interactive viewing
tm_shape(ctry_combined_sf) +
  tm_basemap("Esri.WorldTopoMap") +
  tm_layout("2022 Bad Map - Top 10 Most 
            Dangerous per Institute for Economics and Peace") +
  tm_polygons(col = "Danger.Score", palette = c("#E3735E", "#EC5800", "#C70039"),
              style = "fixed",
              breaks = c(3000, 3100, 3200, 3300, 3400, 3500, 3600),
              legend.hist = TRUE,
              popup.vars = TRUE)